package jplat.tools.coder;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import jplat.tools.file.XFileTools;
import jplat.tools.string.XLog;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

import ebcs.app.auto.transmodel.AppTransferReqModel;

public class JsonCoder
{
	/**
	 * Object to A String.
	 * @param obj
	 * @return
	 */
	public static String toJsonString( Object obj )
	{
		return new Gson().toJson(obj);
	}
	
	/**
	 * Convert json to Object.
	 * @param json
	 * @param classz
	 * @return
	 */
	public static <T> T fromJsonString( String json, Class<T> clazz )
	{
		return new Gson().fromJson(json, clazz);
	}
	
	/**
	 * Convert json to Object.
	 * @author zhangcq
	 * @date Dec 8, 2016
	 * @comment 
	 * @param je
	 * @param clazz
	 * @return
	 */
	public static <T> T fromJson( JsonElement je, Class<T> clazz  )
	{
		return  new Gson().fromJson(je, clazz);
	}
	
	/**
	 * 格式化json
	 * @author zhangcq
	 * @date Jan 6, 2017
	 * @comment
	 */
	public static void prettyJson()
	{
		String filepath = "C:\\Users\\zhangcq\\Desktop\\临时文件夹\\33.json";
		String charset = "gbk";
		Gson gson = new GsonBuilder().setPrettyPrinting().create();
		String uglyJsonStr = XFileTools.loadFile(filepath, charset);
		JsonParser jp = new JsonParser();
		JsonElement je = jp.parse(uglyJsonStr);
		String prettyJsonStr2 = gson.toJson(je);
		System.out.println("prettyJsonStr2: " + prettyJsonStr2);
	}
	
	public static void testGenericJson()
	{
		AppTransferReqModel model = new AppTransferReqModel();
		model.setAcname("Jimmy");
		model.setAcno("1234567890");
		
		Map<String,AppTransferReqModel> map = new HashMap<String,AppTransferReqModel>();
		map.put("mkey1", model);
		String json = JsonCoder.toJsonString(map);
		
		XLog.log("1:%s",json);
		
		map = JsonCoder.fromJsonString(json, Map.class);
//		model = map.get("mkey1");
		
		XLog.log("2:%s",map.get("mkey1"));
		
		Type type = new TypeToken<Map<String,AppTransferReqModel>>() {
		}.getType();
		
		map = new Gson().fromJson(json, type);
//		String clInfo = JsonCoder.toJsonString(type);
//		XLog.log("type=%s", clInfo);
		
//		type=JsonCoder.fromJsonString(clInfo, Type.class);
		XLog.log("model=%s", JsonCoder.toJsonString(map.get("mkey1")));
		
	}
	
	private static void testGenericJson1()
	{
		GTestBean bean = new GTestBean();
		bean.setName("Tomj");
		
		AppTransferReqModel model1 = new AppTransferReqModel();
		model1.setAcname("Lily");
		model1.setAcno("882901");
		
		bean.getDataMap().put("map1", model1);
		bean.getDataMap().put("map2", model1);
		
		String json = JsonCoder.toJsonString(bean);
		
		bean = JsonCoder.fromJsonString(json, GTestBean.class);
		
		XLog.log("GTestBean:%s", JsonCoder.toJsonString(bean));
	}
	
	public static void main( String args[] )
	{
//		testGenericJson();
		testGenericJson1();
	}
}
